#include "stdafx.h"
#include <gtest/gtest.h>
#include <boost/thread.hpp>
#include <database/connection_pool.h>

using namespace Zen;

#if 0
ConnPool* dbpool;

void db_operater(int n)
{
	database* db = ConnPool::GetInstance()->GetConnection();
	printf("%d - 0x%X \n", n, db);
	Sleep(5000);
	if (db) db->close();
	ConnPool::GetInstance()->ReleaseConnection(db);
}

void db_operater_syc(int n)
{
	database* db = ConnPool::GetInstance()->GetConnectionSync(n);
	printf("sync %d - 0x%X \n", n, db);
}


TEST(database, conection_pool)
{
	dbpool = ConnPool::GetInstance();
	dbpool->InitConnPool(L"localhost", 3306, L"root", L"root", L"", 5);

	boost::thread_group tg;
	tg.create_thread(boost::bind(db_operater, 1));
	tg.create_thread(boost::bind(db_operater, 2));
	tg.create_thread(boost::bind(db_operater, 3));
	tg.create_thread(boost::bind(db_operater, 4));
	tg.create_thread(boost::bind(db_operater, 5));
	tg.create_thread(boost::bind(db_operater, 6));
	tg.create_thread(boost::bind(db_operater_syc, 0));
	tg.create_thread(boost::bind(db_operater_syc, 3));
	tg.create_thread(boost::bind(db_operater_syc, 5));
	tg.create_thread(boost::bind(db_operater_syc, 7));
	tg.create_thread(boost::bind(db_operater_syc, 9));
	tg.join_all();

	Sleep(100*1000);
}

#endif